<?php

namespace Worker\Controller;

use Think\Controller;

/**
 * 订单表
 * 1=>待审核 2=>审核未通过 3=>审核通过 4=>上画待验收 5=>下画待验收 6=>订单完成
 * Class OrderController
 * @package Home\Controller
 */
class OrderController extends BaseController
{
    public function __construct()
    {
    }

    /**
     * 城市合伙人给师傅分配订单，此时师傅端才显示未接单
     * 订单列表
     */
    public function orderList()
    {
        $id = I('id');
        //$id = 4;
        $size = I('size', 5);
        $page = I('page', 1);
        $keyword = I('keyword');
        $time = I('time');//2019-03-12
        $type = I('type', 1);//1 未接单 2已接单

        $user = $this->checkUser('worker', $id);
        if (!$user) {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '用户信息有误', 'data' => []]);
        }

        if ($keyword) {
            $where['s.store_name'] = ['like', '%' . $keyword . '%'];
        }

        //查找当天时间的订单
        if ($time) {
            $where['wk_order.add_time'] = ['gt', strtotime($time)];
            $where['wk_order.add_time'] = ['lt', strtotime($time . ' 23:59:59')];
        }
        //当前用户未接单
        if ($type == 1) {
            $where['wk_order.worker_order_status'] = 0;
        }

        //当前用户已接单
        if ($type == 2) {
            $where['wk_order.worker_order_status'] = ['gt', 0];
        }

        $where['wk_order.worker_id'] = $id;
        $tmp = [];
        //$where['partner_id'] = $user['partner_id'];
        $data = $this->getList('order', $where, $size, $page);
        if ($data) {
            foreach ($data as $k => $v) {
                $tmp[$k]['title'] = '店铺广告施工';
                $tmp[$k]['order_no'] = $v['order_no'];   //订单号
                $tmp[$k]['activity_id'] = $v['activity_id'];
                $tmp[$k]['store'] = $v['store_name'];
                $tmp[$k]['address'] = $v['address'];
                $tmp[$k]['update_time'] = $v['update_time'];
                $tmp[$k]['worker_order_status'] = $v['worker_order_status'];
                $tmp[$k]['worker_order_status_msg'] = $this->statusMsg()[$v['worker_order_status']];
                $tmp[$k]['type'] = $type;
                $tmp[$k]['type_msg'] = $type == 1 ? '未接单' : '已接单';

                $ac_info = M("activity")->where(['id'=>$v['activity_id']])->find();

                $tmp[$k]['date_info'] = date('Y-m-d',$ac_info['up_start_time']).'~'.date('Y-m-d',$ac_info['up_end_time']);
            }
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '订单列表', 'data' => $tmp]);
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '数据为空', 'data' => []]);
        }
    }

    /**
     * [acceptOrder description]
     * @return [type] [description]
     */
    public function acceptOrder()
    {
        $order_no = I('order_no');
        $type = I('type');
        $r = M("order")->where(['order_no' => $order_no])->find();
        if (!$r) {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '订单号异常', 'data' => []]);
        }

        //如果是快递配送，直接跳过领取物料流程
        if ($r['service_type'] == 3) {
            $rr = M('order')->where(['order_no' => $order_no])->save(['worker_order_status' => 2, 'update_time' => time()]);
        } else {
            $rr = M('order')->where(['order_no' => $order_no])->save(['worker_order_status' => 1, 'update_time' => time()]);
        }

        if ($rr) {
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '接受订单成功', 'data' => '']);
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '系统异常', 'data' => []]);
        }
    }

    /**
     * 订单流程
     * type=1。worker_order_stats
     * 1=>已接单,待领物料 2=>物料领取 3=>师傅到店上画进行中 4=>上画待验收 5=>上画验收成功 6=>下画师傅到店下画进行中 7=>下画待验收  8=>已完成
     * order_status. 4=>'上画待验收',5=>'下画待验收',6=>'订单完成'
     */
    public function updateOrder()
    {
        $order_no = I('order_no');
        $status = I('status');
        $user_id = I('user_id');

        $r = M("order")->where(['order_no' => $order_no])->find();
        $user = M("worker")->where(['id' => $user_id])->find();
        if (!$r) {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '订单号异常', 'data' => []]);
        }

        if (!$user) {
            $this->ajaxReturn(['status' => false, 'code' => 1003, 'msg' => '用户异常', 'data' => []]);
        }

        $now_status = $r['worker_order_status'];
        //领取物料，待到店
        if ($status == 1 && $now_status == 1) {
            orderWorkerLog($order_no, $status, $user, '师傅已领取物料', 'add');
            $rr = M('order')->where(['order_no' => $order_no])->save(['worker_order_status' => 2, 'update_time' => time()]);
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '师傅已领取物料', 'data' => []]);
        }

        //师傅到店，待上画
        if ($status == 2 && $now_status == 2) {
            orderWorkerLog($order_no, $status, $user, '师傅已到店', 'add');
            $rr = M('order')->where(['order_no' => $order_no])->save(['worker_order_status' => 3, 'update_time' => time()]);
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '师傅已到店', 'data' => []]);
        }

        //上画完成，待验收
        if ($status == 3 && $now_status == 3) {
            orderWorkerLog($order_no, $status, $user, '师傅已完成上画', 'add');
            $rr = M('order')->where(['order_no' => $order_no])->save(['worker_order_status' => 4, 'order_status' => 4, 'update_time' => time()]);
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '师傅已完成上画', 'data' => []]);
        }

        //上画验收完成
        // if($status == 5 && $now_status == 5){
        //     orderLog($order_no, $status, $user, '店长已完成上画验收','add');
        //     $rr = M('order')->where(['order_no'=>$order_no])->save(['worker_order_status'=>5,'update_time'=>time()]);
        // }else{
        //     $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '系统异常', 'data' => []]);
        // }

        //下画进行中
        if ($status == 5 && $now_status == 5) {
            orderWorkerLog($order_no, $status, $user, '下画师傅已到店', 'add');
            $rr = M('order')->where(['order_no' => $order_no])->save(['worker_order_status' => 6, 'update_time' => time()]);
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '下画师傅已到店', 'data' => []]);
        }

        //下画已完成
        if ($status == 6 && $now_status == 6) {
            orderWorkerLog($order_no, $status, $user, '师傅已完成下画', 'add');
            $rr = M('order')->where(['order_no' => $order_no])->save(['worker_order_status' => 7, 'order_status' => 6, 'update_time' => time()]);
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '师傅已完成下画', 'data' => []]);
        }

        $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '系统异常', 'data' => []]);

        // //下画验收完成
        // if($status == 9 && $now_status == 9){
        //     orderLog($order_no, $status, $user, '下画师傅已到店','add');
        //     $rr = M('order')->where(['order_no'=>$order_no])->save(['worker_order_status'=>10,'update_time'=>time()]);
        // }else{
        //     $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '系统异常', 'data' => []]);
        // }
    }


    /**
     * 订单详情
     */
    public function detailByOrderNo()
    {
        $order_no = I('order_no');
        $order = M('order')->where(['order_no' => $order_no])->find();
        if (!$order) {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '订单号异常', 'data' => []]);
        }

        $activity_info = M('activity')->where(['id' => $order['activity_id']])->find();
        $store_info = M('store')->where(['id' => $order['store_id']])->find();
        $audit_info = M('audit')->where(['id' => $store_info['audit_user_id']])->find();
        $ads_info = M('order_ads a')
            ->field('a.id,a.ads_name,a.ads_id,a.width,a.height,a.filepath,a.ads_type,a.materiel_id,a.activity_image_id,a.is_upper_air,a.num,a.remark')
            ->where(['a.order_no' => $order_no])->select();

        foreach ($ads_info as $k => $v) {
            $ads_info[$k]['detail'] = M("order_ads_detail b")
                ->field('b.id,b.ads_name,b.ads_detail_id,b.ads_type,b.x_axis,b.y_axis,b.width,b.height,b.remark,b.materiel_id,b.materiel_name,b.activity_image_id,b.activity_image_url,b.activity_image_name')
                ->where(['order_ads_id' => $v['id']])->select();

            if ($v['ads_type'] == 1) {//自定义广告
                $ads_info[$k]['filepath'] = C('DOMAIN') . '/' . $v['filepath'];
            }
        }

        foreach ($ads_info as $k => $v) {
            if ($v['ads_type'] == 2) {
                $materiel = M('materiel')->where(['id' => $v['materiel_id']])->field('name')->find();
                $activity_image = M('activity_image')->where(['id' => $v['activity_image_id']])->field('image_url,image_name')->find();
                $ads_info[$k]['materiel_name'] = $materiel['name'];
                $ads_info[$k]['activity_image_url'] = $activity_image['image_url'];
                $ads_info[$k]['activity_image_name'] = $activity_image['image_name'];
                $ads_info[$k]['remark'] = $v['remark'];
            }
        }

        $data = [
            'activity_name' => $activity_info['name'],  //活动名称
            'activity_id' => $activity_info['id'],  //活动id
            'order_no' => $order_no,   //订单号
            'order_status' => $order['worker_order_status'], //订单状态
            'order_status_msg' => $this->statusMsg()[$order['worker_order_status']], //订单状态
            'store_id' => $order['store_id'],
            'store_name' => $store_info['store_name'], //门店名字
            'store_manager' => $store_info['manager_name'], //店长名字
            'store_mobiel' => $store_info['mobile'], //店长电话
            'store_address' => $store_info['address'], //上画地址
            'store_longitude' => $store_info['longitude'], //上画地址
            'store_latitude' => $store_info['latitude'], //上画地址
            'audit_name' => $audit_info['name'], //审核员名字
            'audit_mobile' => $audit_info['mobile'], //审核员电话
            'up_time' => $order['up_time'], //上画时间
            'down_time' => $order['down_time'], //下画时间
            'audit_result' => $order['audit_result'], //审核不通过原因
            'is_down' => $order['is_down'], //是否下画
            'is_down_msg' => $order['is_down'] == 0 ? '否' : '是',
            'ads_info' => $ads_info
        ];

        $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '订单详情查询成功', 'data' => $data]);
    }
}